//
//  ExpertListCell.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/1.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "ExpertListCell.h"

@interface ExpertListCell ()

/// viewModle
@property (nonatomic, readwrite, strong) ExpertListCellModel *viewModel;

/// IBOutlet
@property (weak, nonatomic) IBOutlet UIButton* consulationBtn;
@property (weak, nonatomic) IBOutlet UIButton* expertBtn;
@property (weak, nonatomic) IBOutlet UILabel* nameLabel;
@property (weak, nonatomic) IBOutlet UILabel* introLabel;
@property (weak, nonatomic) IBOutlet UIImageView* avatarImgView;

@end

@implementation ExpertListCell

- (void)awakeFromNib
{
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

#pragma mark - bind data
- (void)bindViewModel:(ExpertListCellModel *)viewModel
{
    self.viewModel = viewModel;
    
    @weakify(self);
    
    self.nameLabel.text = self.viewModel.name;
    
    NSString* intro = HCStrFormat(@"专家简介：%@", self.viewModel.intro?self.viewModel.intro:@"暂无简介");
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:intro];
    //修改颜色
    [string addAttribute:NSForegroundColorAttributeName value:UIColorHex(#222222) range:NSMakeRange(0, 5)];
    [string addAttribute:NSForegroundColorAttributeName value:UIColorHex(#888888) range:NSMakeRange(5, intro.length - 5)];
    self.introLabel.attributedText = string;
    
    [self.avatarImgView yy_setImageWithURL:[NSURL URLWithString:self.viewModel.headimg] placeholder:MHImageNamed(@"placeholderLogo")];
    
    [[[[self rac_signalForSelector:@selector(layoutSubviews)] takeUntil:[self rac_prepareForReuseSignal]] delay:0.1f] subscribeNext:^(id x)
    {
        @strongify(self);
        
        self.avatarImgView.layer.cornerRadius = self.avatarImgView.frame.size.width/2.0f;
        self.avatarImgView.layer.masksToBounds = YES;
    }];
    
    self.consulationBtn.rac_command = self.viewModel.consultCommand;
    
    [[self.expertBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x)
    {
        @strongify(self);
        
        [self.viewModel.expertCommand execute:nil];
    }];
    
    [[self.consulationBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x)
     {
         @strongify(self);
         
         [self.viewModel.consultCommand execute:nil];
     }];
}

@end
